﻿@{
    ViewBag.Title = "UserList";
    Layout = "~/Areas/Shared/_spList.cshtml";
}
@model Kaiser.Models.UserBasic
<div data-options="region:'west',split:true" title="所有部门" style="width: 150px;">
    <ul class="easyui-tree" id="Tree">
    </ul>
</div>
<div region="center" border="false">
    <div id="grid">
    </div>
</div>
<div style="display: none">
    <div id="Dlg-CategoryTree" style="width: 350px; height: 220px;">
        <ul class="easyui-tree" id="SelectCategoryTree">
        </ul>
    </div>
    
</div>
<script type="text/javascript">
    // 全局变量
    var grid;
    var dlg_Edit;
    var dlg_Edit_form;
    var searchWin;
    var searchForm;
    var virpath = ""; //网站的虚拟目录 如：/ShopManager
    var config = {
        ListActionURL: '@Url.Action("UserList", "Admin", new { area = "Admin" })',
        AddActionURL: '@Url.Action("AddUser", "Admin", new { area = "Admin" })',
        DelActionURL: '@Url.Action("DelUser", "Admin", new { area = "Admin" })',
        EditActionURL: '@Url.Action("EditUser", "Admin", new { area = "Admin" })'
    };
    var __QueryList = '';

    $(function () {
        //        $('#BookPublisherCode').combogrid({
        //            panelWidth: 500,
        //            url: '@Url.Action("GetBookPublisherList", "BookManage", new { area = "MIS" })',
        //            idField: 'PublisherCode',
        //            textField: 'PublisherName',
        //            mode: 'remote',
        //            fitColumns: true,
        //            columns: [[
        //                    { field: 'PublisherName', title: '出版社名称', width: 60 },
        //                    { field: 'PublisherUrl', title: '出版社主页', width: 80 }
        //                ]]
        //        });

        $('#Tree').tree({
            url: '@Url.Action("GetDeptTree", "DeptManage")',
            onClick: function (node) {
                grid.datagrid({ url: config.ListActionURL, queryParams: { BookCategoryCode: node.id} });
            },
            onLoadSuccess: function () {

                var node = $('#CategoryTree').tree('find', $("#BookCategoryCode").val());
                if (node != null) {
                    $('#CategoryTree').tree('select', node.target);
                }

            }
        });



        grid = $('#grid').datagrid({
            title: '用户管理',
            iconCls: 'icon-save',
            methord: 'post',
            url: config.ListActionURL,
            method: 'post',
            sortName: 'BookID',
            sortOrder: 'desc',
            idField: 'BookID',
            pageSize: 30,
            striped: true,
            queryParams: { BookCategoryCode: 'Root' },
            frozenColumns: [[
	                { field: 'ck', checkbox: true }

				]],

            columns: [[
					{ field: 'UserName', title: '账户', width: 60, sortable: true },
					{ field: 'FullName', title: '姓名', width: 60, sortable: true },
                    { field: 'UserNO', title: '简拼', width: 60, sortable: true },
                    { field: 'UserType', title: '用户类型', width: 60, sortable: true },
                    { field: 'Gender', title: '性别', width: 60, sortable: true },
                    { field: 'National', title: '民族', width: 60, sortable: true },
                    { field: 'IDCard', title: '身份证', width: 60, sortable: true },
                    { field: 'Email', title: '电子邮箱', width: 60, sortable: true },
                    { field: 'AccountState', title: '账号状态', width: 60, sortable: true }

				]],
            fit: true,
            pagination: true,
            rownumbers: true,
            fitColumns: true,
            singleSelect: false,
            toolbar: [{
                text: '新增',
                iconCls: 'icon-add',
                handler: add
            }, '-', {
                text: '修改',
                iconCls: 'icon-edit',
                handler: edit
            }, '-', {
                text: '删除',
                iconCls: 'icon-remove',
                handler: del
            }], onDblClickRow: function (rowIndex, rowData) {  //双击事件
                edit();

            }, onHeaderContextMenu: function (e, field) {
                e.preventDefault();
                if (!$('#tmenu').length) {
                    createColumnMenu();
                }
                $('#tmenu').menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
            }

        });



        dlg_CategoryTree = $('#Dlg-CategoryTree').dialog({
            closed: true,
            modal: true


        });

        $('#btn-search,#btn-search-cancel').linkbutton();
        searchWin = $('#search-window').window({
            iconCls: 'icon-search',
            closed: true,
            modal: true
        });
        searchForm = searchWin.find('form');

        $('body').layout();

    });


    function createColumnMenu() {
        var tmenu = $('<div id="tmenu" style="width:100px;"></div>').appendTo('body');
        var fields = grid.datagrid('getColumnFields');
        for (var i = 0; i < fields.length; i++) {
            $('<div iconCls="icon-ok"/>').html(fields[i]).appendTo(tmenu);
        }
        tmenu.menu({
            onClick: function (item) {
                if (item.iconCls == 'icon-ok') {
                    grid.datagrid('hideColumn', item.text);
                    tmenu.menu('setIcon', {
                        target: item.target,
                        iconCls: 'icon-empty'
                    });
                } else {
                    grid.datagrid('showColumn', item.text);
                    tmenu.menu('setIcon', {
                        target: item.target,
                        iconCls: 'icon-ok'
                    });
                }
            }
        });
    }

    function getSelectedArr() {
        var ids = [];
        var rows = grid.datagrid('getSelections');
        for (var i = 0; i < rows.length; i++) {
            ids.push(rows[i].UserName);
        }
        return ids;
    }
    function getSelectedID() {
        var ids = getSelectedArr();
        return ids.join(',');
    }
    function arr2str(arr) {
        return arr.join(',');
    }

    function add() {
    
        dialog = openWindow({
            title: "新增用户",
            url:virpath + config.AddActionURL,
            height: 250,
            width: 500,
            isIframe: true,
            toolbar: [
                    {
                        text: '保存',
                        iconCls: "icon-save",
                        handler: function () {
                            //alert(dialog.find("iframe")[0].contentWindow.saveData);
                            if (dialog.find("iframe")[0].contentWindow.saveData()) {
                                dialog.dialog("close");
                            }

                        }
                    },
                    '-',
                    {
                        text: '关闭',
                        iconCls: "icon-no",
                        handler: function () {
                            dialog.dialog("close");
                        }
                    }
                ]
        });
        //Msgalert("提示", '成功调用', "info");    
    }

    function edit() {
        var rows = grid.datagrid('getSelections');
        var num = rows.length;
        if (num == 0) {
            Msgshow('请选择一条记录进行操作!');
            //$.messager.alert('提示', '请选择一条记录进行操作!', 'question');       
            return;
        }
        else if (num > 1) {
            Msgfade('您选择了多条记录,只能选择一条记录进行修改!'); //$.messager.alert('提示', '您选择了多条记录,只能选择一条记录进行修改!', 'info');
            return;
        }
        else {
            dialog = openWindow({
                title: "修改用户",
                url: virpath + config.EditActionURL + '?id=' + rows[0].UserName,
                height: 250,
                width: 500,
                isIframe: true,
                toolbar: [
                    {
                        text: '保存',
                        iconCls: "icon-save",
                        handler: function () {
                            //alert(dialog.find("iframe")[0].contentWindow.saveData);
                            if (dialog.find("iframe")[0].contentWindow.saveData()) {
                                dialog.dialog("close");
                            }

                        }
                    },
                    '-',
                    {
                        text: '关闭',
                        iconCls: "icon-no",
                        handler: function () {
                            dialog.dialog("close");
                        }
                    }
                ]
            });

 

        }
    }
    function del() {
        var arr = getSelectedArr();
        //alert(arr);
        if (arr.length > 0) {
            $.messager.confirm('提示信息', '您确认要删除选中的所有记录吗?', function (data) {
                if (data) {
                    $.ajax({
                        url: virpath + config.DelActionURL + '?ids=' + arr2str(arr),
                        type: 'post',
                        error: function () {
                            Msgalert('错误', '删除失败!', 'error');
                            grid.datagrid('clearSelections');
                        },
                        success: function (Result) {
                            var data = eval('(' + Result + ')');
                            if (data.success) {
                                Msgfade(arr.length + "条记录" + data.msg); //提示消息
                                grid.datagrid('reload');
                                grid.datagrid('clearSelections');
                            } else {
                                Msgalert('错误', data.msg, 'error');
                            }
                        }
                    });
                }
            });
        } else {
            Msgshow('请先选择要删除的记录。');

        }
    }
    function saveData() {
        //alert(dlg_Edit_form.url);
        dlg_Edit_form.form('submit', {
            url: dlg_Edit_form.url,
            onSubmit: function () {
                return $(this).form('validate');
            },
            success: successCallback   //~/Content/JqueryUI/MyEasyUIcommmon.js
        });
    }


    function showAll() {
        grid.datagrid({ url: virpath + config.ListActionURL });
    }
    function OpensearchWin() {
        searchWin.window('open');
        searchForm.form('clear');
    }

    function SearchOK() {
        var BookName = $("#S_BookName").val();
        var BookCategoryName = $("#S_BookCategoryName").val();
        searchWin.window('close');
        grid.datagrid({ url: virpath + config.ListActionURL, queryParams: { BookName: BookName, BookCategoryName: BookCategoryName} });
    }
    function closeSearchWindow() {
        searchWin.window('close');
    }
</script>
